package com.freeter.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.freeter.common.utils.DateUtils;
import com.freeter.modules.job.entity.ScheduleJobEntity;
import com.freeter.modules.job.service.ScheduleJobService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;

@Component
public class AbstractTask  implements ITask {

    @Autowired
    protected ScheduleJobService jobService;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected Date getRunParameter(String beanName){
        Wrapper<ScheduleJobEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("bean_name",beanName);
        ScheduleJobEntity scheduleJobEntity = jobService.selectOne(queryWrapper);

        if(StringUtils.isBlank(scheduleJobEntity.getParams())){
            return new Date();
        }else {
            return DateUtils.stringToDate(scheduleJobEntity.getParams(),DateUtils.DATE_PATTERN);
        }

    }

    @Override
    public void run(String params) throws InterruptedException, ParseException {
        logger.info("start job");
    }

    @Override
    public void run() throws InterruptedException, ParseException {
        logger.info("start job");
    }
}
